home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / ANS199A.ZIP / answer.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-25  |  7.1 KB  |  291 lines

  1. // (C) copyright 1996 Sacha Prins
  2.  
  3. #include <fstream.h>
  4. #include <strstrea.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include "\work\modemengine\modemengine.hpp"
  9.  
  10. int bits_per_sample=4;
  11. int device= 1;
  12. char * file_name= NULL;
  13. char message_name[20]= "";
  14. char * com_port= NULL;
  15. BOOL debug= FALSE;
  16.  
  17. void usage();
  18. void parse_arg(int argc, char * argv[]);
  19. void answer();
  20. MODEM_ENGINE::MODEMRESPONSE response (MODEM_ENGINE & me, int timeout=600);
  21.  
  22. //*****************************************************************************
  23. int main (int argc, char * argv[]) {
  24.  
  25.  parse_arg(argc, argv);
  26.  
  27.  cout << "bits per sample: " << bits_per_sample << endl
  28.       << "device         : " << device << endl
  29.       << "file name      : " << file_name << endl
  30.       << "com port       : " << com_port << endl;
  31.  
  32.  
  33.  int i=0;
  34.  while (TRUE) {
  35.    strstream * str= new strstream();
  36.    *str << i << ".msg" << ends;
  37.    strcpy(message_name, str->str());
  38.    answer();
  39.    delete str;
  40.    i++;
  41.  
  42.  } /* endwhile */
  43.  
  44.  
  45. };
  46.  
  47.  
  48. //*****************************************************************************
  49. void answer() {
  50.  
  51.  MODEM_ENGINE me(com_port);
  52.  
  53.  ifstream fin(file_name, ios::bin);
  54.  if (!fin.good()) {
  55.     usage();
  56.     exit(0);
  57.  }
  58.  
  59.  HFILE message;
  60.  ULONG ulAction=0;
  61.  
  62.  if (DosOpen(message_name,
  63.              &message,
  64.              &ulAction,
  65.              0,
  66.              FILE_NORMAL ,
  67.              OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
  68.              OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE,
  69.              0) != NO_ERROR) {
  70.  
  71.     usage();
  72.     exit(0);
  73.  }
  74.  
  75.  if (me.mResultCode () == MODEM_ENGINE::noerror) {
  76.  
  77.     MODEM_ENGINE::MODEMRESPONSE mr;
  78.  
  79.     cout << endl << "Initializing the modem..." << endl;
  80.  
  81.     me.mInitialize();
  82.     response (me);
  83.  
  84.     cout << endl << "Waiting for call..." << endl;
  85.  
  86.     mr= response(me);
  87.     while (mr.code != MODEM_ENGINE::ring) {
  88.        mr= response(me);
  89.     } /* endwhile */
  90.  
  91.     me.vmSetDevice(MODEM_ENGINE::voice);
  92.     response (me);
  93.  
  94.     me.vmSetSpeed();
  95.     response (me);
  96.  
  97.     me.vmSetSilenceDetection(FALSE);
  98.     response (me);
  99.  
  100.     switch (bits_per_sample) {
  101.       case 2:
  102.       me.vmSetBits(MODEM_ENGINE::two);
  103.       break;
  104.       case 3:
  105.       me.vmSetBits(MODEM_ENGINE::three);
  106.       break;
  107.       case 4:
  108.       me.vmSetBits(MODEM_ENGINE::four);
  109.       break;
  110.     } /* endswitch */
  111.  
  112.     response (me);
  113.  
  114.     switch (device) {
  115.      case 1: // Phone line
  116.        me.vmSetLine(MODEM_ENGINE::line);
  117.        response (me);
  118.  
  119.        me.mAnswer();
  120.        response (me);
  121.  
  122.        break;
  123.  
  124.      case 2: // Phone line monitor
  125.        me.vmSetLine(MODEM_ENGINE::lineMonitor);
  126.        response (me);
  127.  
  128.        me.mAnswer();
  129.        response (me);
  130.  
  131.        break;
  132.  
  133.     } /* endswitch */
  134.  
  135.     HFILE com_port;
  136.  
  137.     me.vmTransmit(com_port);
  138.     response (me);
  139.  
  140.     unsigned char buf[1024];
  141.     ULONG cbWritten=0, bitcount=0;
  142.  
  143.     cout << "Initializing done..." << endl << "Playing file..." << endl;
  144.  
  145.     while (!fin.eof()) {
  146.  
  147.        fin.read(buf, sizeof(buf));
  148.        bitcount+= fin.gcount();
  149.        DosWrite(com_port, &buf, fin.gcount(), &cbWritten);
  150.     }
  151.  
  152.     cout << bitcount << " bytes written" << endl;
  153.  
  154.     me.vmStopTransmit();
  155.     mr= response (me, 50);
  156.  
  157.     while (mr.code==MODEM_ENGINE::none) {
  158.        me.vmStopTransmit();
  159.        mr= response (me, 50);
  160.     } /* endwhile */
  161.  
  162.     cout << "Playing done..." << endl;
  163.  
  164.     me.vmBeep();
  165.     response(me);
  166.  
  167.     me.vmReceive(message);
  168.     response(me);
  169.  
  170.     char c;
  171.     c=me.mWaitForDLECode();
  172.  
  173.     while (c != 'q' && c != 'b' && c !='a' && c !='c' && c !='d' && c !='e' && c !='f' && c !='s') {
  174.  
  175.        c=me.mWaitForDLECode();
  176.     } /* endwhile */
  177.  
  178.     me.vmStopReceive();
  179.     mr= response (me, 50);
  180.  
  181.     while (mr.code==MODEM_ENGINE::none) {
  182.        me.vmStopTransmit();
  183.        mr= response (me, 50);
  184.     } /* endwhile */
  185.  
  186.     cout << "Recording done..." << endl << "Resetting the modem..." << endl;
  187.  
  188.     me.mInitialize();
  189.     response (me);
  190.  
  191.     DosClose(message);
  192.  
  193.     cout << "All done..." << endl;
  194.  
  195.  
  196.  } else {
  197.     cout << endl << "Unable to open the '" << com_port << "' device." << endl;
  198.  }
  199. }
  200.  
  201.  
  202. //*****************************************************************************
  203. MODEM_ENGINE::MODEMRESPONSE response (MODEM_ENGINE & me, int timeout) {
  204.  
  205.  MODEM_ENGINE::MODEMRESPONSE mr;
  206.  
  207.     do {
  208.        mr= me.mWaitForModemResponse (timeout);
  209.        if (debug) cout << mr.verbose << endl;
  210.        if (mr.verbose[0]== 0) {
  211.           mr.code= MODEM_ENGINE::none;
  212.        }
  213.     } while ((mr.code != MODEM_ENGINE::ok) &&
  214.              (mr.code != MODEM_ENGINE::error) &&
  215.              (mr.code != MODEM_ENGINE::connect) &&
  216.              (mr.code != MODEM_ENGINE::none) &&
  217.              (mr.code != MODEM_ENGINE::ring) &&
  218.              (mr.code != MODEM_ENGINE::vcon));
  219.  
  220.    return mr;
  221. }
  222.  
  223. //*****************************************************************************
  224. void parse_arg(int argc, char * argv[]) {
  225.  
  226.  for (int i=1; i< argc; i++) {
  227.  
  228.     if (!strncmp(argv[i], "-d:", 3)) {
  229.       device= atoi(argv[i]+3);
  230.       if (device != 1 && device != 2) {
  231.          usage();
  232.          exit(0);
  233.       }
  234.     } else {
  235.      if (!strncmp(argv[i], "-b:", 3)) {
  236.         bits_per_sample= atoi(argv[i]+3);
  237.         if (bits_per_sample != 2 && bits_per_sample != 3 && bits_per_sample != 4) {
  238.            usage();
  239.            exit(0);
  240.         }
  241.         cout << argv[i]+3 << "\n";
  242.      } else {
  243.         if (!strncmp(argv[i], "-f:", 3)) {
  244.  
  245.           file_name= strdup(argv[i]+3);
  246.         } else {
  247.            if (!strncmp(argv[i], "-c:", 3)) {
  248.  
  249.               com_port= strdup(argv[i]+3);
  250.            } else {
  251.               if (!strncmp(argv[i], "-de", 3)) {
  252.  
  253.                  debug= TRUE;
  254.               } else {
  255.  
  256.                 usage();
  257.                 exit(0);
  258.  
  259.               } /* endif */
  260.            } /* endif */
  261.         } /* endif */
  262.      } /* endif */
  263.     } /* endif */
  264.  } /* endfor */
  265.  
  266.  if (file_name==NULL || com_port==NULL) {
  267.     usage();
  268.     exit(0);
  269.  } else {
  270.  } /* endif */
  271.  
  272. };
  273.  
  274.  
  275. //*****************************************************************************
  276. void usage(){
  277.  
  278.  cout << "* Required parameters: " << endl << endl
  279.       << "-f:filename, where 'filename' is the name (+ path) of the greeting file." << endl << endl
  280.       << "-c:comport, where 'comport' is the name of the device the voice modem is" << endl << " connected to." << endl << endl
  281.       << "* Optional parameters:" << endl << endl
  282.       << "-b:bits, where 'bits' is either 2, 3 or 4 and stands for the sampling rate of" << endl << " the file to be played, this defaults to 4" << endl
  283.       << " The bitrate is for the incoming AND outgoing message the same."<<endl
  284.       << "-d:device, where 'device' is either 1, or 2." << endl
  285.       << " 1 stands for telephone line. This defaults to 1" << endl
  286.       << " 2 stands for telephone line with monitor. This defaults to 1" << endl << endl
  287.       << "-debug turns debug info on." << endl << endl
  288.       << "* Example:" << endl << endl
  289.       << "answer -f:c:\\path\\greeting.msg -c:com1 -b:3 -d:2" << endl;
  290. };
  291.